﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ScooterLand
{
    public partial class CustomerForm : Form
    {
        Backend.Program controller = new Backend.Program();
        int clientID = 0;

        public CustomerForm()
        {
            InitializeComponent();
        }

        private void orderItem_Click(object sender, EventArgs e)
        {
            OrderForm oForm = new OrderForm();
            oForm.Show();
            this.Close();
        }

        private void customersItem_Click(object sender, EventArgs e)
        {
            CustomerForm cForm = new CustomerForm();
            cForm.Show();
            this.Close();
        }

        private void productsItem_Click(object sender, EventArgs e)
        {
            ProductForm pForm = new ProductForm();
            pForm.Show();
            this.Close();
        }

        private void employeesItem_Click(object sender, EventArgs e)
        {
            EmployeeForm eForm = new EmployeeForm();
            eForm.Show();
            this.Close();
        }

        private void invoicesItem_Click(object sender, EventArgs e)
        {
            InvoiceForm iForm = new InvoiceForm();
            iForm.Show();
            this.Close();
        }

        private void exitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CustomerForm_Load(object sender, EventArgs e)
        {
            DataTable brandTable = controller.ListBrands();

            cmbSearchBrand.DataSource = brandTable;
            cmbSearchBrand.DisplayMember = "name";
            cmbSearchBrand.ValueMember = "bid";

            // Define default values
            cmbSearchBrand.SelectedValue = 0;
            cmbSearchBrand.SelectedText = "-- Vælg scootermærke --";

            DataTable brandTable2 = controller.ListBrands();
            cmbScooterBrand.DataSource = brandTable2;
            cmbScooterBrand.DisplayMember = "name";
            cmbScooterBrand.ValueMember = "bid";

            // Define default values
            cmbScooterBrand.SelectedValue = 0;
            cmbScooterBrand.SelectedText = "-- Vælg scootermærke --";
        }

        private void btnSearchCustomer_Click(object sender, EventArgs e)
        {
            lvCustomers.Items.Clear();
            int brandID;
            brandID = Convert.ToInt32(cmbSearchBrand.SelectedValue);

            DataTable resultTable = controller.FindCustomers(txtSearchTelephone.Text, txtSearchName.Text, brandID);
            DataRow[] resultRows = resultTable.Select();

            foreach (DataRow row in resultRows)
            {
                string lastOrder = controller.LastOrder(row[0].ToString());
                string[] str = { row[0].ToString(), row[1].ToString(), row[2].ToString(), row[3].ToString(), lastOrder };
                ListViewItem lvItem = new ListViewItem(str);
                lvCustomers.Items.Add(lvItem);
            }
        }

        private void btnSelectCustomer_Click(object sender, EventArgs e)
        {
            lvScooters.Items.Clear();
            lvOrders.Items.Clear();
            txtClientID.Text = "";
            txtFullName.Text = "";
            txtAddress.Text = "";
            txtZipcode.Text = "";
            txtCity.Text = "";
            txtTelephone.Text = "";
            txtEmail.Text = "";

            // Set Client ID
            clientID = Convert.ToInt32(lvCustomers.SelectedItems[0].Text);

            // Get Client Info
            string[] clientArray = controller.GetCustomerInfo(clientID);

            txtClientID.Text = clientArray[0];
            txtFullName.Text = clientArray[1];
            txtAddress.Text = clientArray[2];
            txtZipcode.Text = clientArray[3];
            txtTelephone.Text = clientArray[4];
            txtEmail.Text = clientArray[5];
            txtCity.Text = clientArray[6];

            if (clientID != 0)
            {
                // Load customers scooters
                DataTable scooterTable = controller.FindScooters(clientID);
                DataRow[] scooterRows = scooterTable.Select();

                foreach (DataRow row in scooterRows)
                {
                    string brandID = row["bid"].ToString();
                    string scooterBrand = controller.FindBrand(brandID);
                    string[] strScooter = { scooterBrand, row["registration"].ToString(), row["vin"].ToString() };
                    ListViewItem lvScooter = new ListViewItem(strScooter);
                    lvScooters.Items.Add(lvScooter);
                }

                // Load customers orders
                DataTable orderTable = controller.ListOrders(clientID);
                DataRow[] orderRows = orderTable.Select();

                foreach (DataRow row in orderRows)
                {
                    string status = "";
                    if (row[4].ToString() == "pending")
                        status = "Afventer";
                    else if (row[4].ToString() == "inprogress")
                        status = "Igangværende";
                    else if (row[4].ToString() == "completed")
                        status = "Afsluttet";

                    string mechanic = "";
                    if (row[1].ToString() == "0")
                        mechanic = "Ingen tilknyttet";
                    else
                    {
                        int eid = Convert.ToInt32(row[1].ToString());
                        string[] employeeInfo = controller.GetEmployeeInfo(eid);
                        mechanic = employeeInfo[1] + " (" + employeeInfo[2] + ")";
                    }

                    string[] strOrder = { row[0].ToString(), status, row[2].ToString(), row[3].ToString(), mechanic };
                    ListViewItem lvOrder = new ListViewItem(strOrder);
                    lvOrders.Items.Add(lvOrder);
                }

            }
        }

        private void btnCustomerSave_Click(object sender, EventArgs e)
        {
            bool result = false;

            if (clientID != 0)
            {
                string fullName = txtFullName.Text;
                string address = txtAddress.Text;
                int zipcode = Convert.ToInt32(txtZipcode.Text);
                int telephone = Convert.ToInt32(txtTelephone.Text);
                string email = txtEmail.Text;

                result = controller.UpdateCustomer(clientID, fullName, address, zipcode, telephone, email);

                if (result != true)
                    MessageBox.Show("Handlingen kunne ikke gennemføres");
            }
            else
            {
                string fullName = txtFullName.Text;
                string address = txtAddress.Text;
                int zipcode = Convert.ToInt32(txtZipcode.Text);
                int telephone = Convert.ToInt32(txtTelephone.Text);
                string email = txtEmail.Text;

                controller.CreateCustomer(fullName, address, zipcode, telephone, email, out result, out clientID);

                if (result != true)
                    MessageBox.Show("Handlingen kunne ikke gennemføres");
                else
                {
                    DataTable brandTable = controller.ListBrands();

                    cmbScooterBrand.DataSource = brandTable;
                    cmbScooterBrand.DisplayMember = "name";
                    cmbScooterBrand.ValueMember = "bid";

                    // Define default values
                    cmbScooterBrand.SelectedValue = 0;
                    cmbScooterBrand.SelectedText = "-- Vælg scootermærke --";
                }
            }

            if (result != false)
            {
                string[] clientArray = controller.GetCustomerInfo(clientID);

                txtClientID.Text = clientArray[0];
                txtFullName.Text = clientArray[1];
                txtAddress.Text = clientArray[2];
                txtZipcode.Text = clientArray[3];
                txtTelephone.Text = clientArray[4];
                txtEmail.Text = clientArray[5];
                txtCity.Text = clientArray[6];


            }

        }

        string scooterVIN = "";

        private void btnScooterSave_Click(object sender, EventArgs e)
        {
            bool result = false;
            int brandID = 0;

            if (scooterVIN != "")
            {
                brandID = Convert.ToInt32(cmbScooterBrand.SelectedValue.ToString());
                string registration = txtRegistration.Text;
                string VIN = txtVIN.Text;

                result = controller.UpdateScooter(VIN, clientID, registration, brandID);

                if (result != true)
                    MessageBox.Show("Der skete en fejl under opdateringen af scooteren. Tjek venligst om stelnummeret tilhører en anden scooter.");
                else
                {
                    lvScooters.Items.Remove(lvScooters.SelectedItems[0]);

                    string scooterBrand = controller.FindBrand(brandID.ToString());
                    string[] strScooter = { scooterBrand, txtRegistration.Text, txtVIN.Text };
                    ListViewItem lvScooter = new ListViewItem(strScooter);
                    lvScooters.Items.Add(lvScooter);

                    scooterVIN = "";
                    txtVIN.ReadOnly = false;
                    txtVIN.Text = "";
                    txtRegistration.Text = "";
                    cmbScooterBrand.SelectedValue = 0;
                    cmbScooterBrand.Text = "-- Vælg scootermærke --";
                }

            }
            else
            {
                brandID = Convert.ToInt32(cmbScooterBrand.SelectedValue.ToString());
                result = controller.CreateScooter(txtVIN.Text, clientID, txtRegistration.Text, brandID);

                if (result != true)
                    MessageBox.Show("Det lykkedes ikke at tilføje scooteren. Tjek venligst om stelnummeret allerede er registreret.");
                else
                {
                    string scooterBrand = controller.FindBrand(brandID.ToString());
                    string[] strScooter = { scooterBrand, txtRegistration.Text, txtVIN.Text };
                    ListViewItem lvScooter = new ListViewItem(strScooter);
                    lvScooters.Items.Add(lvScooter);

                    txtVIN.Text = "";
                    txtRegistration.Text = "";
                    cmbScooterBrand.SelectedValue = 0;
                    cmbScooterBrand.Text = "-- Vælg scootermærke --";
                }
            }

        }

        private void btnScooterUpdate_Click(object sender, EventArgs e)
        {
            txtVIN.ReadOnly = true;

            scooterVIN = lvScooters.SelectedItems[0].SubItems[2].Text;
            string[] scooterArray = controller.GetScooterInfo(scooterVIN);
            txtVIN.Text = scooterArray[0];
            txtRegistration.Text = scooterArray[2];

            string brandName = controller.FindBrand(scooterArray[3]);
            cmbScooterBrand.SelectedIndex = cmbScooterBrand.FindStringExact(brandName);
        }

        private void btnScooterRemove_Click(object sender, EventArgs e)
        {
            scooterVIN = lvScooters.SelectedItems[0].SubItems[2].Text;
            bool result = controller.RemoveScooter(scooterVIN);

            if (result != true)
                MessageBox.Show("Det lykkedes ikke at fjerne den valgte scooter. Kontroller venligst stelnummeret.");
            else
            {
                scooterVIN = "";
                lvScooters.Items.Remove(lvScooters.SelectedItems[0]);
            }
        }

        private void btnNewOrder_Click(object sender, EventArgs e)
        {
            OrderForm oForm = new OrderForm(clientID);
            oForm.Show();
            this.Close();
        }

        private void btnUpdateOrder_Click(object sender, EventArgs e)
        {
            int orderID = Convert.ToInt32(lvOrders.Items[0].SubItems[0].Text);
            OrderForm oForm = new OrderForm(clientID, orderID);
            oForm.Show();
            this.Close();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            int orderID = Convert.ToInt32(lvOrders.Items[0].SubItems[0].Text);
            InvoiceForm iForm = new InvoiceForm(orderID);
            iForm.Show();
            this.Close();
        }
    }
}
